# -*- coding: utf-8 -*-
# Houdini To Arnold
# Copyright (C) 2013 Erkan Ozgur Yilmaz
#
# This file is part of Houdini To Arnold.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

import sys
import soho
import logging
import tempfile
import os
import ArnoldOptions, ArnoldFilter, ArnoldCamera, ArnoldGeo, ArnoldShader
import ArnoldLight

reload(ArnoldOptions)
reload(ArnoldFilter)
reload(ArnoldCamera)
reload(ArnoldGeo)
reload(ArnoldShader)
reload(ArnoldLight)

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

log_file = os.path.join(tempfile.gettempdir(), 'h2a.log')
fHandler = logging.FileHandler(log_file)
logger.addHandler(fHandler)



#hou.ui.displayMessage('Hello world')


# Evaluate the 'camera' parameter as a string.
# If the 'camera' parameter
# doesn't exist, use ['/obj/cam1'].
# SOHO always returns lists of values.
camera_path = soho.getDefaultedString('camera', ['/obj/cam1'])[0]

# However, for our purposes, we don't actually need a camera, so...
#camera = None

# Evaluate an intrinsic parameter (see HDK_SOHO_API::evaluate())
# The 'state:time' parameter evaluates the time from the ROP.
evaltime = soho.getDefaultedFloat('state:time', [0.0])[0]

logger.debug('evaltime: %s' % evaltime)

# Initialize SOHO with the camera.
if not soho.initialize(evaltime, camera_path):
    soho.error('Unable to initialize rendering module with camera: %s' %
               repr(camera_path))

# Now, add objects to our scene
#    addObjects(time, geometry, light, fog, use_display_flags)
soho.addObjects(evaltime, "*", "*", "*", True)

# Before we can evaluate the scene from SOHO, we need to lock the object lists.
soho.lockObjects(evaltime)

#def output_data(file_handler, prefix, obj_list):
def output_data(file_handler, prefix='', data=''):
    # Now, traverse all the objects
    #file_handler.write('%s\n{' % prefix)
    #for obj in data:
    #    file_handler.write('"%s",' % obj.getName())
    #file_handler.write('}\n')
    file_handler.write(data)
    file_handler.flush()

fp = sys.stdout
#output_data(fp, 'Geometry', soho.objectList('objlist:instance'))
#output_data(fp, 'Light', soho.objectList('objlist:light'))
#output_data(fp, 'Fog', soho.objectList('objlist:fog'))

output_data(
    fp,
    'Options',
    ArnoldOptions.export_options({
        'xres': 640,
        'yres': 480,
        'aspect': 1.0,
        'cameraName': ArnoldCamera.get_camera_name(camera_path),
        'binary_ass': 'off'
    })
)

output_data(fp, 'Filter', ArnoldFilter.generate_data({}))
output_data(fp, 'Camera', ArnoldCamera.generate_data_from_path(camera_path))
output_data(fp, 'Shader', ArnoldShader.generate_data({}))
output_data(fp, 'Light', ArnoldLight.generate_data({}))

for obj in soho.objectList('objlist:instance'):
    name = obj.getName()
    logger.debug('obj_path : %s' % name)
    output_data(fp, 'Geometry', ArnoldGeo.export_geometry(name))


